export interface RecordAttributes {
  type: string;
  url?: string;
}

export type Record<T = any> = {
  Id?: string;
  attributes: RecordAttributes | undefined;
} & T;

export type ExistingRecord = {
  Id: string;
} & Record;

export type ExtendableRecord<T = any> = Record<T> & { withFields: (fields: { [key: string]: string }) => Record };

export const recordOf = (type: string): ExtendableRecord => ({
  ...attributedType(type),
  withFields: (fields: { [key: string]: string }): Record => ({
    ...attributedType(type),
    ...fields,
  }),
});

export const recordToString = (record: Record) => {
  const fieldValues = Object.entries(record).filter(([key, _]) => !['Id', 'attributes'].includes(key));
  record.attributes.type;
  if (fieldValues.length) {
    return `${record.attributes.type} record with fields: ${JSON.stringify(Object.fromEntries(fieldValues), null, 2)}`;
  }
  return `${record.attributes.type} record`;
};

const attributedType = (type: string) => ({
  attributes: {
    type,
  },
});
